package com.giscloud.amp.controller.auth;

import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.dto.AmpStaffMenuDataDTO;
import com.giscloud.amp.entity.vo.AmpStaffMenuDataVo;
import com.giscloud.amp.service.auth.AmpStaffMenuDataService;
import com.giscloud.commons.model.Result;
import com.giscloud.token.utils.TokenUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author:gxd
 * @Description:
 * @Date: 17:24 2018/2/24
 * @Modified By:
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
public class AmpStaffMenuDataController extends BaseController {
    @Autowired
    private AmpStaffMenuDataService ampStaffMenuDataRemoteClient;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    /**
     * 根据条件获取菜单信息
     * @return
     */
    @RequestMapping("/amp/ampStaffMenuData/findAuthDataByMenuId")
    public Result findAuthDataByMenuId(@RequestParam(value = "menuIdList[]",required = false) List<Long> menuIdList,
                                       @RequestBody AmpStaffMenuDataDTO ampStaffMenuDataDTO){
        ampStaffMenuDataDTO.setMenuIds(menuIdList);
        List<AmpStaffMenuDataVo> ampStaffMenuDataVos = ampStaffMenuDataRemoteClient.findAuthDataByMenuId(ampStaffMenuDataDTO);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("menuDataList",ampStaffMenuDataVos);
        return Result.success(map);
    }

    @RequestMapping(value="/amp/ampStaffMenuData/saveStaffMenuData",method = RequestMethod.POST)
    public Result saveStaffMenuData(String menuIds,String unCkAuthIds,String ckAuthIds,AmpStaffMenuDataDTO ampStaffMenuDataDTO){
        try {
            String[] menuIdArr = menuIds.split(",");
            String[] ckAuthIdArrs = ckAuthIds.split("\\|");
            String[] unkAuthIdArrs = unCkAuthIds.split("\\|");
            Map<Long,Map<String,List<Long>>> menuAuth = new HashMap<>();
            for (int i=0;i<menuIdArr.length;i++){
                Map<String,List<Long>> authMap = new HashMap<>();
                String[] ckAuthIdArr = null;
                String[] unkAuthIdArr = null;
                if(!ckAuthIdArrs[i].equals("0")){
                    ckAuthIdArr =  ckAuthIdArrs[i].split(",");
                }
                if(!unkAuthIdArrs[i].equals("0")){
                    unkAuthIdArr =  unkAuthIdArrs[i].split(",");
                }
                if(ckAuthIdArr!=null){
                    authMap.put("ckAuthIds",stringAsLong(ckAuthIdArr));
                }
                if(unkAuthIdArr!=null){
                    authMap.put("unkAuthIds",stringAsLong(unkAuthIdArr));
                }
                menuAuth.put(Long.parseLong(menuIdArr[i]),authMap);
            }
            ampStaffMenuDataDTO.setMenuAuth(menuAuth);
            ampStaffMenuDataDTO.setCreatedStaffId(tokenUserUtil.getUserId());
            ampStaffMenuDataDTO.setOpreateStaffId(tokenUserUtil.getUserId());
            ampStaffMenuDataRemoteClient.saveStaffMenuData(ampStaffMenuDataDTO);
            return Result.success("","保存成功");
        }catch (Exception e){
            return Result.error(-1,"保存失败");
        }
    }

    private List<Long> stringAsLong(String[] strs){
        ArrayList<Long> list = new ArrayList<>();
        for(String item : strs){
            list.add(Long.valueOf(item));
        }
        return list;
    }

}
